/*
 * @(#)BuyMod.java 2011-12-09
 *
 * Copyright 2011 北龙中网（北京）科技有限责任公司. All rights reserved.
 */
package cn.shangjia.shop.controller.mod;

import java.util.Date;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.shangjia.common.dao.support.Page;
import cn.shangjia.common.domain.BuyOnline;
import cn.shangjia.common.util.DateUtils;
import cn.shangjia.common.util.StringUtils;
import cn.shangjia.shop.controller.base.AbstractController;
import cn.shangjia.shop.exception.NotFoundException;
import cn.shangjia.shop.service.BuyService;
import cn.shangjia.shop.service.impl.BuyServiceImpl;
import cn.shangjia.shop.service.v2.impl.BuyServiceImpl4Solr;

/**
 *
 * @author <a href="mailto:jinzhongze@knet.cn">jinzhongze</a>
 * @version 1.0, 2011-12-09 14:15:41
 * @since 1.0
 */
@Controller
public class BuyMod extends AbstractController implements ApplicationContextAware{
	
	private BuyService buyService;
	
	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		boolean isSolr = config.getIsSolr().booleanValue();
		buyService = applicationContext.getBean(isSolr ? BuyServiceImpl4Solr.class : BuyServiceImpl.class);
	}
	
	/**
	 *  求购 主页面
	 * 
	 * @param model
	 * @param dir
	 * @param pageno
	 * @author <a href="mailto:jinzhongze@knet.cn">jinzhongze</a>
	 */
	@RequestMapping("/shop/module/buy/index.html")
	public String mod_buy(ModelMap model,String userid, String dir, String pageNo){
		if (StringUtils.isEmpty(userid))
			throw new NotFoundException("非法请求, 您所请求的页面不存在");
		Page<BuyOnline> page = buyService.findBuyPage(userid,dir, getInt(pageNo), config.getBuyCurrentPagesize());
		model.addAttribute("page", page);
		model.addAttribute("dir", dir);
		return "shop/module/buy/index";
	}
	

	/**
	 *  求购终端页
	 * 
	 * @param model
	 * @param userid
	 * @param id
	 * @return
	 * @author <a href="mailto:jinzhongze@knet.cn">jinzhongze</a>
	 */
	@RequestMapping("/shop/module/buy/detail.html")
	public String mod_buy_detail(ModelMap model, String userid, String id) {
		if (StringUtils.isEmpty(id))
			return mod_buy(model, userid, null, null);
		BuyOnline buy = buyService.getBuyById(id);
		if (buy == null) 
			return mod_buy(model, userid, null, null);
		model.addAttribute("buyInfo", buy);
		model.addAttribute("intervalDay", DateUtils.intervalDay(buy.getExpired(), new Date()));
		return "shop/module/buy/detail";
	}


}
